package com.archstoday.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.archstoday.R;
import com.base.utils.LogUtil;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;

import java.io.File;

public class PdfActivity extends AsBaseActivity implements OnPageChangeListener {

    private TextView mTitleTv;
    private PDFView mPdfView;
    private TextView mMaskLayout, mToActivityBtn;

    private int isFree;

    @Override
    protected void initView(Bundle bundle) {
        setContentView(R.layout.activity_pdf);
        mTitleTv = (TextView) findViewById(R.id.title_tv);
        mPdfView = (PDFView) findViewById(R.id.pdf_view);
        mMaskLayout = (TextView) findViewById(R.id.pdf_mask_layout);
        mToActivityBtn = (TextView) findViewById(R.id.pdf_to_activity);
    }

    @Override
    protected void initData(Bundle bundle) {
        mTitleTv.setText("原文件");
        isFree = getIntent().getIntExtra("isFree", 0);
        if (isFree == 0) {
            mToActivityBtn.setVisibility(View.VISIBLE);
        }

        File file = (File) getIntent().getSerializableExtra("file");
        mPdfView.fromFile(file)
                .defaultPage(1)
                .onPageChange(this)
                .load();
    }

    @Override
    protected void initListener() {
        setOnClickListener(R.id.back_btn);
        setOnClickListener(R.id.pdf_to_activity);
    }

    @Override
    public void treatClickEvent(int id) {
        switch (id) {
            case R.id.back_btn:
                appFinish();
                break;
            case R.id.pdf_to_activity:
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse(getIntent().getStringExtra("web"));
                intent.setData(content_url);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onPageChanged(int page, int pageCount) {
        LogUtil.log_e("HX", "page=====>"+page);
        LogUtil.log_e("HX", "pageCount=====>"+pageCount);
        if (isFree == 0) {
            if (pageCount > 40) {
                if (page > (pageCount * 0.35)) {
                    mMaskLayout.setVisibility(View.VISIBLE);
                }
            } else if (pageCount > 10) {
                if (page > (pageCount * 0.4)) {
                    mMaskLayout.setVisibility(View.VISIBLE);
                }
            } else if (pageCount > 5) {
                if (page > 5) {
                    mMaskLayout.setVisibility(View.VISIBLE);
                }
            }
        }
    }
}